package luanvan.bus;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import cse.luanvan.R;

public class BaseBusAdapterInstruction extends BaseAdapter {

	private static ArrayList<PathBus> listPathbus;
	private static final int[] imageID = { R.drawable.dibo, R.drawable.bus };

	LayoutInflater layoutInflater;

	public BaseBusAdapterInstruction(Context mcontext, ArrayList<PathBus> list) {
		super();
		this.layoutInflater = LayoutInflater.from(mcontext);
		listPathbus = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listPathbus.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listPathbus.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.instruction, null);
			viewholder = new ViewHolder();
			viewholder.image = (ImageView) convertView.findViewById(R.id.image);
			viewholder.txtView = (TextView) convertView
					.findViewById(R.id.content);
			convertView.setTag(viewholder);
		} else
			viewholder = (ViewHolder) convertView.getTag();

		String instruction = "";
		PathBus bus = listPathbus.get(position);
		if (listPathbus.get(position).tuyen.MaTuyen.equals("0")) {
			viewholder.image.setImageResource(imageID[0]);
			instruction += "Đi bộ đến trạm (" + bus.tramCuoi.MaTram + ") "
					+ bus.tramCuoi.TenTram+"     " +String.format("%.2f", bus.KhoangCach/1000) + " km";
		} else {
			viewholder.image.setImageResource(imageID[1]);
			instruction +="Đón xe "+bus.tuyen.TenTuyen +" đến trạm ("
						+ bus.tramCuoi.MaTram + ") " + bus.tramCuoi.TenTram +"   "+String.format("%.2f", bus.KhoangCach/1000) + " km";
		}
		viewholder.txtView.setText(instruction);
		return convertView;
	}

	static class ViewHolder {
		ImageView image;
		TextView txtView;

	}

}
